package cn.itcast.bos.web.action.base;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{

	//模型驱动
	private Standard standard=new Standard();
	
	@Override
	public Standard getModel() {
		return standard;
	}

	@Autowired
	private StandardService standardService;
	
	//保存收派标准
	@Action(value="standard_save",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String saveStandard(){
		
		standardService.save(standard);
		
		return SUCCESS;
	}
	
	//属性驱动来获取分页查询自动提交的数据
	private int page;//当前页码
	private int rows;//每页条数

	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	//分页查询收派标准数据
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		Pageable pageable=new PageRequest(page-1, rows);
		
		Page<Standard> pageData=standardService.pageQuery(pageable);
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", pageData.getTotalElements());
		map.put("rows", pageData.getContent());
		
		//以值栈的方式把数据放到valuestack最上面,然后返回到页面
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	//增加快递员是弹窗中收派标准的显示,查询所有收派标准,并把收派标准name属性显示在弹出框中收派标准的下拉框
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		List<Standard> standards=standardService.findAll();
		ActionContext.getContext().getValueStack().push(standards);
		return SUCCESS;
	}
	
	
	
	
}
